package dk.mirasola.systemtraining.bridgewidgets.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Card;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Hand;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Suit;
import dk.mirasola.systemtraining.bridgewidgets.shared.util.CardUtil;

public class HandPanel extends Composite {
    interface HandPanelUiBinder extends UiBinder<Widget, HandPanel> {}
    private static HandPanelUiBinder uiBinder = GWT.create(HandPanelUiBinder.class);

    @UiField
    HTML header;
    @UiField
    HTML spades;
    @UiField
    HTML hearts;
    @UiField
    HTML diamonds;
    @UiField
    HTML clubs;
    @UiField
    HTML shade;
    @UiField
    HTML heart;
    @UiField
    HTML diamond;
    @UiField
    HTML club;

    public HandPanel() {
        this(true);
    }

    public HandPanel(boolean visibleHeader) {
        initWidget(uiBinder.createAndBindUi(this));
        shade.setHTML(RenderUtil.renderSuit(Suit.SPADE));
        heart.setHTML(RenderUtil.renderSuit(Suit.HEART));
        diamond.setHTML(RenderUtil.renderSuit(Suit.DIAMOND));
        club.setHTML(RenderUtil.renderSuit(Suit.CLUB));
        header.setVisible(visibleHeader);
    }


    public void setHeader(SafeHtml seat) {
        header.setHTML(seat);
    }

    public void setHand(Hand hand) {
        if (hand == null) {
            spades.setHTML("");
            hearts.setHTML("");
            diamonds.setHTML("");
            clubs.setHTML("");
        } else {
            SafeHtmlBuilder sb = new SafeHtmlBuilder();
            for (Card card : CardUtil.sort(hand.getCards(Suit.SPADE))) {
                RenderUtil.renderRangShort(card.getRang(), sb);
            }
            spades.setHTML(sb.toSafeHtml());

            sb = new SafeHtmlBuilder();
            for (Card card : CardUtil.sort(hand.getCards(Suit.HEART))) {
                RenderUtil.renderRangShort(card.getRang(), sb);
            }
            hearts.setHTML(sb.toSafeHtml());

            sb = new SafeHtmlBuilder();
            for (Card card : CardUtil.sort(hand.getCards(Suit.DIAMOND))) {
                RenderUtil.renderRangShort(card.getRang(), sb);
            }
            diamonds.setHTML(sb.toSafeHtml());

            sb = new SafeHtmlBuilder();
            for (Card card : CardUtil.sort(hand.getCards(Suit.CLUB))) {
                RenderUtil.renderRangShort(card.getRang(), sb);
            }
            clubs.setHTML(sb.toSafeHtml());
        }

    }


}
